package com.lyy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyy.Vo.RoleMenu;
import com.lyy.Vo.RoleVo;
import com.lyy.handler.BusinessException;
import com.lyy.response.ResultCode;
import com.lyy.system.entity.Role;
import com.lyy.system.enums.UserStatusEnum;
import com.lyy.system.mapper.RoleMapper;
import com.lyy.system.mapper.RoleMenuMapper;
import com.lyy.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public IPage<Role> findRolePage(Page<Role> page, QueryWrapper<Role> wrapper) {
        return this.baseMapper.findRolePage(page,wrapper);
    }

    @Override
    public void addRole(Role role) {
        //判断角色名称是否存在
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_name",role.getRoleName()).eq("status","1");
        Integer count=this.baseMapper.selectCount(wrapper);
        if (count!=0){
            throw new BusinessException(ResultCode.ROLENAME_EXIST.getCode(),
                    ResultCode.ROLENAME_EXIST.getMessage());
        }
        //设置状态
        role.setStatus(UserStatusEnum.AVAILABLE.getStatusCode());
        this.baseMapper.insert(role);
    }

    @Override
    public void deleteById(Long id) {
        Role role =new Role();
        role.setStatus(0);
        role.setModifiedTime(new Date());
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        this.baseMapper.update(role,wrapper);
    }

    @Override
    public void update(RoleVo roleVo) {
        //判断角色名称是否存在
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_name",roleVo.getRoleName()).eq("status","1");
        Integer count=this.baseMapper.selectCount(wrapper);
        if (count!=0){
            throw new BusinessException(ResultCode.ROLENAME_EXIST.getCode(),
                    ResultCode.ROLENAME_EXIST.getMessage());
        }
        Role role=new Role();
        role.setId(roleVo.getId());
        role.setRoleName(roleVo.getRoleName());
        role.setRemark(roleVo.getRemark());
        role.setModifiedTime(new Date());
        this.baseMapper.updateById(role);
    }

    @Override
    public void menuAuth(List<RoleMenu> roleMenus) {
        if (!CollectionUtils.isEmpty(roleMenus)){
            QueryWrapper<RoleMenu> wrapper =new QueryWrapper<>();
            wrapper.eq("role_id",roleMenus.get(0).getRoleId());
            roleMenuMapper.delete(wrapper);
            for (RoleMenu roleMenu:roleMenus){
                RoleMenu roleMen =new RoleMenu();
                roleMen.setRoleId(roleMenu.getRoleId());
                roleMen.setMenuId(roleMenu.getMenuId());
                roleMenuMapper.insert(roleMen);
            }
        }else {
            throw new BusinessException(ResultCode.LIST_IS_NOTNULL.getCode(),
                    ResultCode.LIST_IS_NOTNULL.getMessage());
        }
    }

    @Override
    public Role roleDetail(Long id) {
        Role role =this.baseMapper.selectById(id);
        return role;
    }


    @Override
    public Role getRoleByUserId(QueryWrapper<Role> wrapper) {
        Role role =this.baseMapper.getRoleByUserId(wrapper);
        return role;
    }

}
